﻿using System;
using System.IO;
using System.Xml;

namespace ZYCommon
{
   

    public class SimpleXMLConfig : XmlDocument
    {
        private string strFileName = null;

        public string GetSetting(string strName)
        {
            XmlElement element = base.SelectSingleNode("*/setting[@name='" + strName + "']") as XmlElement;
            if (element == null)
            {
                return null;
            }
            return element.InnerText;
        }

        public new bool  Load(string filename)
        {
            bool flag=false;
            try
            {
                this.strFileName = filename;
                base.Load(filename);
                flag = true;
            }
            catch
            {
            }
            return flag;
        }

        public void Save()
        {
            if (this.strFileName != null)
            {
                if (File.Exists(this.strFileName))
                {
                    File.SetAttributes(this.strFileName, FileAttributes.Normal);
                }
                base.Save(this.strFileName);
            }
        }

        public void SetSetting(string strName, string strValue)
        {
            XmlElement newChild = base.SelectSingleNode("*/setting[@name='" + strName + "']") as XmlElement;
            if (newChild == null)
            {
                if (base.DocumentElement == null)
                {
                    base.AppendChild(base.CreateElement("xmlconfig"));
                }
                bool flag = false;
                foreach (XmlNode node in base.DocumentElement.ChildNodes)
                {
                    if (node.Name == "setting")
                    {
                        newChild = node as XmlElement;
                        if (newChild.GetAttribute("name") == strName)
                        {
                            flag = true;
                            break;
                        }
                    }
                }
                if (!flag)
                {
                    newChild = base.CreateElement("setting");
                    newChild.SetAttribute("name", strName);
                    base.DocumentElement.AppendChild(newChild);
                }
            }
            newChild.InnerText = strValue;
        }

        public string FileName
        {
            get
            {
                return this.strFileName;
            }
            set
            {
                this.strFileName = value;
            }
        }
    }
}

